package com.liuzhilong.infrastructure.framework.algorithms.datastruct.graph;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Objects;

/**
 * <h1>图-边定义</h1>
 * 封装图的边，存有目标顶点和权重信息
 */
@Data
@Accessors(chain = true)
public class Edge<T>  implements Comparable<Edge<T>>{
    //起始
    private Vertex<T> from;
    //目标
    private Vertex<T> to;


    //权重
    private double weight;


    @Override
    public String toString() {
        return "Edge{" +
                "from=" + from +
                ", to=" + to +
                ", weight=" + weight +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {

            return true;
        }

        if (o == null || getClass() != o.getClass()) {

            return false;
        }
        Edge edge = (Edge) o;
        return Objects.equals(from, edge.from) &&
                Objects.equals(to, edge.to);
    }

    @Override
    public int hashCode() {
        return Objects.hash(from, to);
    }


    //创建
    public static final Edge create(Vertex from,Vertex to,double weight){
        return new Edge().setFrom(from).setTo(to).setWeight(weight);
    }
    public static final Edge create(Vertex to,double weight){
        return create(null,to,weight);
    }
    public static final Edge create(Vertex to){
        return create(null,to,1);
    }

    /**
     * 根据传入的顶点，获取另一个顶点
     * @param self
     * @return
     */
    public Vertex<T> other(Vertex<T> self){

        return Objects.equals(self, from) ? to : from;

    }
    @Override
    public int compareTo(Edge<T> o) {
        if(o.getWeight()==getWeight()){
            return 0;
        }
        return o.getWeight() > getWeight() ? -1 : 1;
    }
}
